package com.texaco.chapasFutbol.buttons;

import java.util.ArrayList;
import java.util.List;

import org.andengine.entity.scene.Scene;
import org.andengine.entity.text.AutoWrap;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.opengl.font.Font;
import org.andengine.util.HorizontalAlign;

import com.texaco.chapasFutbol.entidades.Game;

/**
 * Clase que implementa los botones de opciones que vamos a usar en nuestra app.
 * 
 * @author texaco
 * 
 */
public class OptionButton {

	private static final int MARGIN_X = 5;
	private static final int MARGIN_Y = 5;
	List<Button> buttons;
	Text caption;
	Scene scene;

	public OptionButton(Game game, Scene scene, float x, float y,
			int captionId, int textId, Font fontCaption, Font fontValue,
			final IButtonCallBack backButtonCallBack,
			final IButtonCallBack forwardButtonCallBack) {
		this(game, scene, x, y, (String) game.getActivity().getText(captionId),
				(String) game.getActivity().getText(textId), fontCaption,
				fontValue, backButtonCallBack, forwardButtonCallBack);
	}

	public OptionButton(Game game, Scene scene, float x, float y,
			int captionId, String textStr, Font fontCaption, Font fontValue,
			final IButtonCallBack backButtonCallBack,
			final IButtonCallBack forwardButtonCallBack) {
		this(game, scene, x, y, (String) game.getActivity().getText(captionId),
				textStr, fontCaption,
				fontValue, backButtonCallBack, forwardButtonCallBack);
	}

	public OptionButton(Game game, Scene scene, float x, float y,
			String captionStr, String textStr, Font fontCaption,
			Font fontValue, final IButtonCallBack backButtonCallBack,
			final IButtonCallBack forwardButtonCallBack) {
		super();
		this.scene = scene;

		buttons = new ArrayList<Button>();

		// Retroceso
		buttons.add(new Button(game, scene, x, y + fontCaption.getLineHeight()
				+ MARGIN_Y, (String)null, null, Game.OPTIONS_MINUS_SHAPE,
				new IButtonCallBack() {
					public String fire() {
						setValue(backButtonCallBack.fire());
						return getValue();
					}
				}));

		// Valor
		buttons.add(new Button(game, scene, x + MARGIN_X
				+ buttons.get(0).getWidth(), y + fontCaption.getLineHeight()
				+ MARGIN_Y, textStr, fontValue, Game.OPTIONS_VALUE_SHAPE, null));

		// Avance
		buttons.add(new Button(game, scene, x + (MARGIN_X * 2)
				+ buttons.get(0).getWidth() + buttons.get(1).getWidth(), y
				+ fontCaption.getLineHeight() + MARGIN_Y, (String)null, null,
				Game.OPTIONS_PLUS_SHAPE, new IButtonCallBack() {
					public String fire() {
						setValue(forwardButtonCallBack.fire());
						return getValue();
					}
				}));

		caption = new Text(x + MARGIN_X + buttons.get(0).getWidth(), y, fontCaption, captionStr, captionStr.length(),
				new TextOptions(AutoWrap.WORDS, buttons.get(1).getWidth(), HorizontalAlign.CENTER, 0),
				game.getActivity().getVertexBufferObjectManager());

		scene.attachChild(caption);
	}

	public void setValue(String value) {
		buttons.get(1).setText(value);
	}

	public String getValue() {
		return buttons.get(1).getText();
	}

	public void dispose() {
		for (Button button : buttons) {
			button.dispose();
		}
		scene.detachChild(caption);
		caption.dispose();
	}

	public void attach() {
		for (Button button : buttons) {
			button.attach();
		}
		if(!caption.hasParent()){
			scene.attachChild(caption);
		}
	}

	public void detach() {
		for (Button button : buttons) {
			button.detach();
		}
		scene.detachChild(caption);
	}
}
